layui.config({
    base: ctx + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index'         //主入口模块
}).use(['index', 'table', 'util'], function () {
    var prefix = ctx + '/manage/log/log';
    var $ = layui.$, form = layui.form, table = layui.table, util = layui.util;

    //监听搜索
    form.on('submit(LAY-search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-table', {
            where: field
        });
    });

    //事件
    var active = {
        batchdel: function () {
            // 遍历所有选择的行数据，取每条数据对应的ID
            var checkStatus = table.checkStatus('LAY-table')
                , checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return commonJs.openMsg('warning', '请选择要删除的数据！');
            }
            var ids = new Array();
            $.each(checkData, function (i, row) {
                ids[i] = row['id'];
            });

            commonJs.openConfirm('确定删除吗？', function () {
                commonJs.openAjax(prefix + "/batchRemove", {"ids": ids}, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', '删除成功！', function () {
                            table.reload('LAY-table'); //数据刷新
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        }
    };

    $('.layui-btn.layuiadmin-btn-operate').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    var cols = [{
        type: "checkbox",
        fixed: "left"
    }, {
        field: "id",
        title: "ID",
        width: 80
    }, {
        field: "username",
        width: 130,
        title: "用户名称"
    }, {
        field: "operation",
        width: 150,
        title: "用户操作"
    }, {
        field: "time",
        width: 60,
        title: "用时"
    }, {
        field: "method",
        title: "请求方法"
    }, {
        field: "ip",
        width: 120,
        title: "IP地址"
    }, {
        field: "createDate",
        width: 200,
        title: "创建时间"
    }, {
        title: "操作",
        width: 100,
        align: "center",
        fixed: "right",
        toolbar: "#table-btn-operate"
    }];

    //初始化layui的table
    commonJs.tableRender(table, cols, prefix + "/list");

    //初始化layui的table的tool操作
    commonJs.tableTool(table, prefix);

});
